package com.github.wui.comm;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.github.wui.R;


public class RingProgressView extends View {

    private final Paint bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private final Paint arcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private final Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    private float radius;          // 环半径
    private float strokeWidth;     // 环宽
    private int bgColor;           // 背景色
    private int progressColor;     // 进度色
    private float textSize;        // 文字大小
    private int textColor;         // 文字颜色

    private int max = 100;
    private int progress = 0;

    private final RectF arcRect = new RectF();

    public RingProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RingProgressView);
        radius = ta.getDimension(R.styleable.RingProgressView_ringRadius, 80f);
        strokeWidth = ta.getDimension(R.styleable.RingProgressView_ringWidth, 8f);
        bgColor = ta.getColor(R.styleable.RingProgressView_ringBgColor, 0xFFE0E0E0);
        progressColor = ta.getColor(R.styleable.RingProgressView_ringProgressColor, 0xFF3F51B5);
        textSize = ta.getDimension(R.styleable.RingProgressView_textSize, 36f);
        textColor = ta.getColor(R.styleable.RingProgressView_textColor, Color.BLACK);
        progress = ta.getInt(R.styleable.RingProgressView_progress, 0);
        ta.recycle();

        bgPaint.setStyle(Paint.Style.STROKE);
        bgPaint.setStrokeWidth(strokeWidth);
        bgPaint.setColor(bgColor);

        arcPaint.setStyle(Paint.Style.STROKE);
        arcPaint.setStrokeWidth(strokeWidth * 2.5f);
        arcPaint.setColor(progressColor);
        arcPaint.setStrokeCap(Paint.Cap.ROUND);
        arcPaint.setStrokeJoin(Paint.Join.ROUND);

        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setTextSize(textSize);
        textPaint.setColor(textColor);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int size = (int) (radius * 2 + strokeWidth * 2 + getPaddingLeft() + getPaddingRight() + 10);
        setMeasuredDimension(size, size);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float centerX = getWidth() / 2f;
        float centerY = getHeight() / 2f;
        arcRect.set(centerX - radius, centerY - radius,
                centerX + radius, centerY + radius);

        // 背景圆环
        canvas.drawCircle(centerX, centerY, radius, bgPaint);

        // 进度弧
        float sweepAngle = 360f * progress / max;
        canvas.drawArc(arcRect, -90, sweepAngle, false, arcPaint);


    }

    public void setProgress(int progress) {
        this.progress = Math.min(Math.max(progress, 0), max);
        postInvalidate();
    }

    public void setProgressWithAnim(int progress) {
        ValueAnimator anim = ValueAnimator.ofInt(this.progress, progress);
        anim.setDuration(600);
        anim.addUpdateListener(animation -> {
            setProgress((int) animation.getAnimatedValue());
        });
        anim.start();
    }

    public void setMax(int max) {
        this.max = max;
        postInvalidate();
    }

    public Paint getBgPaint() {
        return bgPaint;
    }

    public Paint getArcPaint() {
        return arcPaint;
    }

    public Paint getTextPaint() {
        return textPaint;
    }

    public float getRadius() {
        return radius;
    }

    public void setRadius(float radius) {
        this.radius = radius;
        postInvalidate();
    }

    public float getStrokeWidth() {
        return strokeWidth;
    }

    public void setStrokeWidth(float strokeWidth) {
        this.strokeWidth = strokeWidth;
        postInvalidate();
        postInvalidate();
    }

    public int getBgColor() {
        return bgColor;
    }

    public void setBgColor(int bgColor) {
        this.bgColor = bgColor;
        postInvalidate();
    }

    public int getProgressColor() {
        return progressColor;
    }

    public void setProgressColor(int progressColor) {
        this.progressColor = progressColor;
        postInvalidate();
    }

    public float getTextSize() {
        return textSize;
    }

    public void setTextSize(float textSize) {
        this.textSize = textSize;
        postInvalidate();
    }

    public int getTextColor() {
        return textColor;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
        postInvalidate();
    }

    public int getMax() {
        return max;
    }

    public int getProgress() {
        return progress;
    }


}